package com.libraries;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// external enum class; we can create also internal enum class in page
enum userRole { ADMIN, EMPLOYEE }

public class Page {

	private String headerContent;
	private String pageContent;
	
	// JSP objects
	private HttpServletRequest requestObject;
	private HttpServletResponse responseObject;
	private ArrayList<String> JSList;

	public Page() {
		JSList = new ArrayList<String>();
	}
	
	public void drawHeader(String pageTitle) {
		
		PrintWriter out;
		
		headerContent = "<html>" +
						"	<head>";
		
		headerContent+= "<link rel='stylesheet' type='text/css' href='pages/css/style.css' />";
		headerContent+= "<script language='javascript' type='text/javascript' src='pages/js/jquery.js' ></script>";		
		headerContent+= "	<title>" + pageTitle + "<title>";
		headerContent+= "	</head>";
	
		try {
			out = responseObject.getWriter();
			out.println(headerContent);
			listJsList();
		} catch (IOException e) {			
			e.printStackTrace();
		}				
	}
	
	public void setJS(String jsFile) {	
		JSList.add(jsFile);
	}
	
	public void listJsList() {
		
		PrintWriter out;
		
			try {				
				out = responseObject.getWriter();
				for(String jsf: JSList) {
					out.println("<script language='javascript' type='text/javascript' src='" + jsf + "' ></script>");
				}
			} catch (IOException e) { 
				e.printStackTrace();
			}	
	}
	
	public void drawPageContent(String pagePath) {
		
		PrintWriter out;
		
		pageContent = "<body>";
		pageContent+= "	<div id='pageHeader'>";
		pageContent+= "	<p>Page logo</p>";		
		pageContent+= "	</div>";		
		try {
			out = responseObject.getWriter();
			out.println(pageContent);
			drawMenu();
		} catch (IOException e) {			
			e.printStackTrace();	
			// I can redirect and add information to my own error object
		}	

		try {
			requestObject.getRequestDispatcher("/pages/admin/views/managementList.jsp").include (requestObject,responseObject);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
		
		pageContent="add footer</body>";
		pageContent+= "</html>";
		
		try {
			out = responseObject.getWriter();
			out.println(pageContent);
		} catch (IOException e) {			
			e.printStackTrace();
		}	
		
	}
	
	private void drawMenu() {
		
		// get user role from session
		// if session role = admin then below;, else get different role and assign
		userRole role = userRole.ADMIN;
		
		switch (role) {
		  case ADMIN:
			    drawAdminMenu();
			  	break;
		  case EMPLOYEE:
			  	drawAdminMenu();
			  	break;		 
		  default:		    			  
			  	break;	  
		}
	}
		
	private void drawAdminMenu() {
		
		// local variables
		String adminMenu = "";
		PrintWriter out;
		
		adminMenu+="<div>" +
				"<ul>" +
				"<li>orders</li>" +
				"<li>bills</li>" +
				"<li>logout</li>" +
				"</ul>" +
				"</div>";		
		try {
			out = responseObject.getWriter();
			out.println(adminMenu);
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
	}
	
	
	// setters
	
	public void setRequestObj(HttpServletRequest request) {	
		this.requestObject = request;
	}
	
	public void setResponseObj(HttpServletResponse response) {
		this.responseObject = response;
	}
	
	// getters
	
	public HttpServletRequest getRequestObj() {
		return this.requestObject;
	}
	
	public HttpServletResponse getResponseObj() {
		return this.responseObject;
	}
	
	
	
	

}
